home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11311 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: surfnet.nl!sun4nl!xs4all!falstaff
  2. From: falstaff@xs4all.nl (Falstaff)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with scanf
  5. Date: 22 Mar 1996 22:27:46 GMT
  6. Organization: XS4ALL, networking for the masses
  7. Message-ID: <4iv9h2$5a3@news.xs4all.nl>
  8. References: <4iscgb$8h9@newsflash.concordia.ca>
  9. NNTP-Posting-Host: xs1.xs4all.nl
  10. X-Newsreader: NN version 6.5.0 #666 (NOV)
  11.  
  12. v_ruso@alcor.concordia.ca writes:
  13.  
  14. >I am writing a program that is supposed to gather information from the
  15. >user.
  16. >The program works fine if the user enters some numbers and presses
  17. >enter.  But if the user just presses enter without entering any data,
  18. >the cursor just starts a new line.  
  19. >Can anyone tell me how I can avoid this?
  20.  
  21. You can use fgets() instead to read a line of text from the user
  22. up to the next newline into a buffer, then process the contents
  23. of the buffer (for example, using sscanf).
  24. Note that if the user types more than the buffer can hold,
  25. the string does not end with a linefeed, and you should
  26. throw the extra input away (perhaps with a while(getchar()) loop.
  27.  
  28. I *never* use fgets() in interactive programs, instead I use
  29. a GetString() function I wrote that limits the user to typing less
  30. than some specified number of characters (fgets won't stop the user
  31. typing too much, even though it prevents the buffer from overflowing).
  32.  
  33. Frank
  34. --
  35. The famous GIICM now on line:  http://www.xs4all.nl/~falstaff/GIICM.html
  36. ------------------------------------------------------------------------
  37. Frank A. Vorstenbosch        +31-(70)-355 5241        falstaff@xs4all.nl
  38.